﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace block_explorer
{
    class FogEffect
    {
        private const string sEffectFilename = "Effects/Fog";

        private const string sDefaultTechniqueName = "Fog";

        private const string sHalfPixelParameterName = "halfPixel";
        private const string sInverseProjectionParameterName = "InverseProjection";
        private const string sDepthMapParameterName = "depthMap";
        private const string sStartParameterName = "FogStart";
        private const string sEndParameterName = "FogEnd";
        private const string sColorParameterName = "FogColor";

        private Effect mEffect;

        private EffectParameter mHalfPixel;
        private EffectParameter mInverseProjection;
        private EffectParameter mDepthMap;
        private EffectParameter mStart;
        private EffectParameter mEnd;
        private EffectParameter mColor;

        public Vector2 HalfPixel { set { mHalfPixel.SetValue(value); } }
        public Matrix InverseProjection { set { mInverseProjection.SetValue(value); } }
        public Texture2D DepthMap { set { mDepthMap.SetValue(value); } }
        public float Start { set { mStart.SetValue(value); } }
        public float End { set { mEnd.SetValue(value); } }
        public Color Color { set { mColor.SetValue(value.ToVector3()); } }

        public FogEffect(GraphicsDevice device, ContentManager content)
        {
            mEffect = content.Load<Effect>(sEffectFilename);

            mHalfPixel = mEffect.Parameters[sHalfPixelParameterName];
            mInverseProjection = mEffect.Parameters[sInverseProjectionParameterName];
            mDepthMap = mEffect.Parameters[sDepthMapParameterName];
            mStart = mEffect.Parameters[sStartParameterName];
            mEnd = mEffect.Parameters[sEndParameterName];
            mColor = mEffect.Parameters[sColorParameterName];

            mEffect.CurrentTechnique = mEffect.Techniques[sDefaultTechniqueName];
        }

        public void Apply()
        {
            mEffect.CurrentTechnique.Passes[0].Apply();
        }
    }
}
